﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Collections.ObjectModel;

namespace WpfPdfReader
{
    /// <summary>
    /// WindowBookManager.xaml 的交互逻辑
    /// </summary>
    public partial class WindowBookManager : Window
    {
        public event EventHandler<BookMarkEventArgs> GoToBookMark;

        public WindowBookManager()
        {
            InitializeComponent();
        }

        public WindowBookManager(List<BookMark> bookMarkList)
            : this()
        {
            this.DataContext = new BookMarkCollection(bookMarkList);
            this.Loaded += new RoutedEventHandler(WindowBookManager_Loaded);

        }

        void WindowBookManager_Loaded(object sender, RoutedEventArgs e)
        {
            if (this.listBox.Items.Count > 0)
            {
                this.listBox.SelectedIndex = 0;
            }
            else
            {
                this.btnModify.IsEnabled = false;
                this.btnSave.IsEnabled = false;
            }
        }

        class BookMarkCollection
        {
            ObservableCollection<BookMark> bookMarks = new ObservableCollection<BookMark>();
            private List<BookMark> bookMarkList = new List<BookMark>();
            public BookMarkCollection(List<BookMark> bookMarks)
            {
                this.bookMarkList = bookMarks;
                this.bookMarks = new ObservableCollection<BookMark>(bookMarks);
            }

            public bool Remove(BookMark item)
            {
                bool isDeleted = this.bookMarks.Remove(item);
                isDeleted = this.bookMarkList.Remove(item);
                return isDeleted;
            }

            public ObservableCollection<BookMark> BookMarks
            {
                get
                {
                    return this.bookMarks;
                }
                set { this.bookMarks = value; }
            }


        }

        private void listBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (this.listBox.SelectedIndex > -1)
            {
                if (this.listBox.SelectedItem == null) return;

                BookMark bookMark = this.listBox.SelectedItem as BookMark;
                this.image.Source = new BitmapImage(new Uri(bookMark.PageBmpPath, UriKind.Absolute));
                this.note.Text = bookMark.Note;
                this.btnModify.IsEnabled = true;
                menuGoto.Header = "转到第" + (bookMark.Page + 1) + "页";
            }
            else
            {
                this.btnModify.IsEnabled = false;
                this.btnSave.IsEnabled = false;
                this.image.Source = null;
                this.note.Text = "";
            }
        }

        private void GotoImage_MouseEnter(object sender, MouseEventArgs e)
        {
            Image GotoImage = sender as Image;
            GotoImage.Source = BrushHelper.GetBitmapImage(new Uri("images/RightArrowLong_Orange.png", UriKind.Relative));
        }

        private void GotoImage_MouseLeave(object sender, MouseEventArgs e)
        {
            Image GotoImage = sender as Image;
            GotoImage.Source = BrushHelper.GetBitmapImage(new Uri("images/RightArrowLong_Green.png", UriKind.Relative));
        }

        private void btnModify_Click(object sender, RoutedEventArgs e)
        {
            this.note.IsReadOnly = false;
            this.note.Focus();
            this.btnModify.IsEnabled = false;
            this.btnSave.IsEnabled = false;
        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            BookMark bookMark = this.listBox.SelectedItem as BookMark;
            if (bookMark != null)
            {
                bookMark.Note = this.note.Text.Trim();
            }
            else
            {
                MessageBox.Show("您还没有添加书签，无法保存", "提示", MessageBoxButton.OK, MessageBoxImage.Exclamation);
            }
            this.note.IsReadOnly = true;
            this.btnModify.IsEnabled = true;
            this.btnSave.IsEnabled = false;
        }

        private void menuGoto_Click(object sender, RoutedEventArgs e)
        {
            if (this.GoToBookMark != null)
            {
                this.GoToBookMark(sender, new BookMarkEventArgs(this.listBox.SelectedItem as BookMark));
                this.Close();
            }
        }

        private void menuDelete_Click(object sender, RoutedEventArgs e)
        {
            BookMark bookMark = this.listBox.SelectedItem as BookMark;
            if (bookMark != null)
            {
                BookMarkCollection bookMarkCollection = this.DataContext as BookMarkCollection;
                bookMarkCollection.Remove(bookMark);
                if (this.listBox.Items.Count > 0)
                    this.listBox.SelectedIndex = 0;
                else
                    this.listBox.SelectedIndex = -1;
            }
        }

        private void Goto(object sender, ExecutedRoutedEventArgs e)
        {
            if (this.GoToBookMark != null)
            {
                this.GoToBookMark(sender, new BookMarkEventArgs(this.listBox.SelectedItem as BookMark));
                this.Close();
            }
        }

        private void CanGotoCommand(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = this.listBox.SelectedItem != null;
        }

        private void Delete(object sender, ExecutedRoutedEventArgs e)
        {
            BookMark bookMark = this.listBox.SelectedItem as BookMark;
            if (bookMark != null)
            {
                BookMarkCollection bookMarkCollection = this.DataContext as BookMarkCollection;
                bookMarkCollection.Remove(bookMark);
                if (this.listBox.Items.Count > 0)
                    this.listBox.SelectedIndex = 0;
                else
                    this.listBox.SelectedIndex = -1;
            }
        }

        private void CanDeleteCommand(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = this.listBox.SelectedItem != null;
        }

        private void note_TextChanged(object sender, TextChangedEventArgs e)
        {
            this.btnSave.IsEnabled = e.Changes != null && e.Changes.Count > 0;
        }

        private void image_MouseUp(object sender, MouseButtonEventArgs e)
        {
        }
    }

    public class DelegateCommand : ICommand
    {
        private readonly Predicate<object> _canExecute;
        private readonly Action<object> _execute;

        public event EventHandler CanExecuteChanged;

        public DelegateCommand(Action<object> execute)
            : this(execute, null)
        {
        }

        public DelegateCommand(Action<object> execute,
                       Predicate<object> canExecute)
        {
            _execute = execute;
            _canExecute = canExecute;
        }

        public bool CanExecute(object parameter)
        {
            if (_canExecute == null)
            {
                return true;
            }

            return _canExecute(parameter);
        }

        public void Execute(object parameter)
        {
            _execute(parameter);
        }

        public void RaiseCanExecuteChanged()
        {
            if (CanExecuteChanged != null)
            {
                CanExecuteChanged(this, EventArgs.Empty);
            }
        }
    }

    public class BookMarkEventArgs : EventArgs
    {
        public int Page { get; private set; }
        public BookMark BookMark { get; set; }
        public BookMarkEventArgs(BookMark bookMark)
        {
            this.BookMark = bookMark;
            this.Page = bookMark.Page;
        }
    }
}
